package st.mark.quadtris.states;

import java.io.IOException;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.loading.DeferredResource;
import org.newdawn.slick.loading.LoadingList;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import st.mark.quadtris.Quadtris;

public class Loading extends BasicGameState {

	private int STATE_ID = -1;
	
	private boolean all_loaded = false;
	
	private Image background = null;
	private Image loadingBar = null;
	
	private String loading = "";
	
	public Loading(int state){
		this.STATE_ID = state;
	}
	
	public int getID() {
		return STATE_ID;
	}

	public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
		LoadingList.get();
		LoadingList.setDeferredLoading(true);
		
		background = new Image("st/mark/resources/loading/loading-main.png");
		loadingBar = new Image("st/mark/resources/loading/loading-bar.png");
		
		// -- Loading
		// Game
		new AngelCodeFont("st/mark/resources/game/font.fnt", "st/mark/resources/game/font.png");
		new Image("st/mark/resources/game/bg.png");
		new Image("st/mark/resources/game/blocks.png");
		new Image("st/mark/resources/game/arrow_down.png");
		new Image("st/mark/resources/game/arrow_up.png");
		new Image("st/mark/resources/game/arrow_left.png");
		new Image("st/mark/resources/game/arrow_right.png");
		new Image("st/mark/resources/game/shapes.png");
		
		// Highscores
		new Image("st/mark/resources/highscores/highscore_bg.png");
		new Image("st/mark/resources/highscores/loading.png");
		new AngelCodeFont("st/mark/resources/highscores/hi-scores.fnt", "st/mark/resources/highscores/hi-scores.png");
		
		// Menu
		new Image("st/mark/resources/menu/menu_bg.png");
		new Image("st/mark/resources/menu/menu_shapes.png");
		new Image("st/mark/resources/menu/play_quadtris.png");
		new Image("st/mark/resources/menu/play_normal.png");
		new Image("st/mark/resources/menu/highscores.png");
		new Image("st/mark/resources/menu/controls.png");
		new Image("st/mark/resources/menu/sound.png");
		new Image("st/mark/resources/menu/exit.png");
		new Image("st/mark/resources/menu/credits.png");
		new Image("st/mark/resources/menu/controls-text.png");
		new Image("st/mark/resources/menu/credits-text.png");
		new Image("st/mark/resources/menu/click_to_close.png");
		
		// Pause Menu
		new Image("st/mark/resources/pausemenu/paused_menu.png");
		new Image("st/mark/resources/pausemenu/resume.png");
		new Image("st/mark/resources/pausemenu/end_game.png");
		new Image("st/mark/resources/pausemenu/main_menu.png");
		new Image("st/mark/resources/pausemenu/sliders.png");
		new Image("st/mark/resources/pausemenu/slider_bar.png");
		new Image("st/mark/resources/pausemenu/menu_pointer.png");
		
		// Audio
		new Sound("st/mark/resources/audio/click.wav");
		new Sound("st/mark/resources/audio/down.wav");
		new Sound("st/mark/resources/audio/rotate.wav");
		new Sound("st/mark/resources/audio/line.wav");
		
		new Music("st/mark/resources/audio/music_1.ogg");
		new Music("st/mark/resources/audio/music_2.ogg");
		new Music("st/mark/resources/audio/music_3.ogg");
		new Music("st/mark/resources/audio/music_4.ogg");
	}

	public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {	
        
		background.draw(0, 0);
		
		int total = LoadingList.get().getTotalResources(); 
        int loaded = LoadingList.get().getTotalResources() - LoadingList.get().getRemainingResources(); 
         
        float bar = (loaded / (float) total); 
        
        g.drawString("Loading... " + loading, 30, 290);
        loadingBar.getSubImage(0, 0, (int)(loadingBar.getWidth()*bar), loadingBar.getHeight()).draw(24, 312);
	}

	public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
		if(LoadingList.get().getRemainingResources() == 0)
			all_loaded = true;
		
		DeferredResource resource = LoadingList.get().getNext();
		
		try {
			if(resource != null) {
				resource.load();
				String[] str = resource.getDescription().split("/");
				loading = str[str.length-1];
			}
		} catch (IOException e) {}
		
		int loaded = LoadingList.get().getTotalResources() - LoadingList.get().getRemainingResources(); 
		
		if(loaded == 0){
			all_loaded = true;
		}
		
		if(all_loaded){
			LoadingList.setDeferredLoading(false);
			sbg.enterState(Quadtris.MENU_STATE);
		}	
	}
}
